package com.rjwh.dingdong.client.activity.more;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.bean.localbean.UserOuterAccountListData;
import com.rjwh.dingdong.client.bean.localbean.XiaomiAccountBeanData;
import com.rjwh.dingdong.client.mvp.presenter.impl.MoreBindSocialAccountPresenter;
import com.rjwh.dingdong.client.mvp.view.MoreBindSocialAccountView;
import com.rjwh.dingdong.client.thirdpartylogin.LoginApi;
import com.rjwh.dingdong.client.thirdpartylogin.OnLoginListener;
import com.rjwh.dingdong.client.thirdpartylogin.OnXiaomiLoginListener;
import com.rjwh.dingdong.client.thirdpartylogin.XiaomiLoginManager;
import com.rjwh.dingdong.client.util.LogUtil;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * @author Kay
 * @ClassName MoreBindSocialAccountActivity
 * @Description
 * @date 2016-10-17
 * @history 2016-10-17 author: description:
 */
public class MoreBindSocialAccountActivity extends BaseActivity implements MoreBindSocialAccountView {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.bind_social_account_weixin_tv)
    TextView bindSocialAccountWeixinTv;
    @BindView(R.id.bind_social_account_qq_tv)
    TextView bindSocialAccountQqTv;
    @BindView(R.id.bind_social_account_weibo_tv)
    TextView bindSocialAccountWeiboTv;
    @BindView(R.id.bind_social_account_xiaomi_tv)
    TextView bindSocialAccountXiaomiTv;
    @BindView(R.id.bind_social_account_xiaomi_root)
    RelativeLayout bindSocialAccountXiaomiRoot;

    private MoreBindSocialAccountPresenter presenter;
    private int weixinIsbind;
    private int qqIsbind;
    private int weiboIsbind;
    private int xiaomiIsbind;

    private String weixinRelativeid;
    private String qqRelativeid;
    private String weiboRelativeid;
    private String xiaomiRelativeid;

    private int appType;//平台类型
    private String uid;
    private XiaomiLoginManager xiaomiLoginManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_bind_social_account_layout);
        ButterKnife.bind(this);
        initView();
        presenter = new MoreBindSocialAccountPresenter(this, this);
        presenter.doGetUserOuterAccountList();
    }

    private void initView() {
        initToolbarHelper();
        try {
            ApplicationInfo appInfo = this.getPackageManager()
                    .getApplicationInfo(getPackageName(),
                            PackageManager.GET_META_DATA);
            String msg = appInfo.metaData.getString("BaiduMobAd_CHANNEL");
            LogUtil.e("MoreBindSocialAccountActivity", "msg:"+msg);
            if (!AsStrUtils.isEmpty(msg) && msg.equals("xiaomi")) {
                bindSocialAccountXiaomiRoot.setVisibility(View.VISIBLE);
            } else {
                bindSocialAccountXiaomiRoot.setVisibility(View.GONE);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(getResources().getString(R.string.person_data_bind_social_account));
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @OnClick({R.id.bind_social_account_weixin_root, R.id.bind_social_account_qq_root, R.id.bind_social_account_weibo_root, R.id.bind_social_account_xiaomi_root})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bind_social_account_weixin_root:
                appType = 1;
                if (weixinIsbind == 1) {
                    doUnBind("weixin", weixinRelativeid);
                } else {
                    LoginApi aip = new LoginApi();
                    aip.setPlatform(Wechat.NAME);
                    aip.setOnLoginListener(new OnLoginListener() {

                        @Override
                        public boolean onLogin(String platform,
                                               HashMap<String, Object> res) {
                            LogUtil.e("ThirdPartyLoginActivity", "weixin onLogin");
                            Platform plat = ShareSDK.getPlatform(platform);
                            uid = plat.getDb().getUserId();
                            String nickname = plat.getDb().getUserName();
                            String avatar = plat.getDb().getUserIcon();
                            String apptype = "weixin";
                            doBindAccount(uid, nickname, avatar, apptype);
                            LogUtil.e("ThirdPartyLoginActivity", "／／name:　" + nickname + "//uid: " + uid + "//icon: " + avatar);
                            return false;
                        }

                        @Override
                        public void onLoadingDailog() {
                            showLoadDialog();
                        }

                        @Override
                        public void onDismissDailog() {
                            dismissLoadDialog();
                        }
                    });
                    aip.login(this);
                }
                break;
            case R.id.bind_social_account_qq_root:
                appType = 2;
                if (qqIsbind == 1) {
                    doUnBind("qq", qqRelativeid);
                } else {
                    LoginApi aip1 = new LoginApi();
                    aip1.setPlatform(QQ.NAME);
                    aip1.setOnLoginListener(new OnLoginListener() {

                        @Override
                        public boolean onLogin(String platform,
                                               HashMap<String, Object> res) {
                            LogUtil.e("ThirdPartyLoginActivity", "weixin onLogin");
                            Platform plat = ShareSDK.getPlatform(platform);
                            uid = plat.getDb().getUserId();
                            String nickname = plat.getDb().getUserName();
                            String avatar = plat.getDb().getUserIcon();
                            String apptype = "qq";
                            doBindAccount(uid, nickname, avatar, apptype);
                            LogUtil.e("ThirdPartyLoginActivity", "／／name:　" + nickname + "//uid: " + uid + "//icon: " + avatar);
                            return false;
                        }

                        @Override
                        public void onLoadingDailog() {
                            showLoadDialog();
                        }

                        @Override
                        public void onDismissDailog() {
                            dismissLoadDialog();
                        }
                    });
                    aip1.login(this);
                }
                break;
            case R.id.bind_social_account_weibo_root:
                appType = 3;
                if (weiboIsbind == 1) {
                    doUnBind("weibo", weiboRelativeid);
                } else {
                    LoginApi aip2 = new LoginApi();
                    aip2.setPlatform(SinaWeibo.NAME);
                    aip2.setOnLoginListener(new OnLoginListener() {

                        @Override
                        public boolean onLogin(String platform,
                                               HashMap<String, Object> res) {
                            LogUtil.e("ThirdPartyLoginActivity", "weixin onLogin");
                            Platform plat = ShareSDK.getPlatform(platform);
                            uid = plat.getDb().getUserId();
                            String nickname = plat.getDb().getUserName();
                            String avatar = plat.getDb().getUserIcon();
                            String apptype = "weibo";
                            doBindAccount(uid, nickname, avatar, apptype);
                            LogUtil.e("ThirdPartyLoginActivity", "／／name:　" + nickname + "//uid: " + uid + "//icon: " + avatar);
                            return false;
                        }

                        @Override
                        public void onLoadingDailog() {
                            showLoadDialog();
                        }

                        @Override
                        public void onDismissDailog() {
                            dismissLoadDialog();
                        }
                    });
                    aip2.login(this);
                }
                break;

            case R.id.bind_social_account_xiaomi_root:
                //小米帐号登录
                appType = 4;
                if (xiaomiIsbind == 1) {
                    doUnBind("weibo", xiaomiRelativeid);
                } else {
                    xiaomiLoginManager = new XiaomiLoginManager(this);
                    xiaomiLoginManager.setOnXiaomiLoginListener(new OnXiaomiLoginListener() {
                        @Override
                        public void onXiaomiLogin(XiaomiAccountBeanData data) {
                            if (data != null) {
                                uid = data.getUserId();
                                String nickname = data.getMiliaoNick();
                                String avatar = data.getMiliaoIcon();
                                String apptype = "xiaomi";
                                doBindAccount(uid, nickname, avatar, apptype);
                                LogUtil.e("ThirdPartyLoginActivity", "／／name:　" + nickname + "//id: " + uid + "//icon: " + avatar);
                            }
                        }

                        @Override
                        public void onLoadingDailog() {
                            showLoading();
                        }

                        @Override
                        public void onDismissDailog() {
                            dismissLoadDialog();
                        }
                    });
                    xiaomiLoginManager.doLogin();
                }
                break;
        }
    }

    @Override
    public void loadData(UserOuterAccountListData weixin, UserOuterAccountListData qq, UserOuterAccountListData weibo, UserOuterAccountListData xiaomi) {
        weixinIsbind = weixin.getIsbind();
        if (!AsStrUtils.isEmpty(weixin.getRelativeid())) {
            weixinRelativeid = weixin.getRelativeid();
        }

        qqIsbind = qq.getIsbind();
        if (!AsStrUtils.isEmpty(qq.getRelativeid())) {
            qqRelativeid = qq.getRelativeid();
        }

        weiboIsbind = weibo.getIsbind();
        if (!AsStrUtils.isEmpty(weibo.getRelativeid())) {
            weiboRelativeid = weibo.getRelativeid();
        }
        xiaomiIsbind = xiaomi.getIsbind();
        if (!AsStrUtils.isEmpty(xiaomi.getRelativeid())) {
            xiaomiRelativeid = xiaomi.getRelativeid();
        }

        if (weixinIsbind == 1) {
            bindSocialAccountWeixinTv.setText("已绑定");
        } else {
            bindSocialAccountWeixinTv.setText("未绑定");
        }
        if (qqIsbind == 1) {
            bindSocialAccountQqTv.setText("已绑定");
        } else {
            bindSocialAccountQqTv.setText("未绑定");
        }
        if (weiboIsbind == 1) {
            bindSocialAccountWeiboTv.setText("已绑定");
        } else {
            bindSocialAccountWeiboTv.setText("未绑定");
        }
        if (xiaomiIsbind == 1) {
            bindSocialAccountXiaomiTv.setText("已绑定");
        } else {
            bindSocialAccountXiaomiTv.setText("未绑定");
        }
    }

    /**
     * 操作成功后设置数据
     *
     * @param type 操作类型 0表示解绑成功;1表示绑定成功
     */
    @Override
    public void setData(int type) {
        switch (appType) {
            case 1:
                weixinIsbind = type;
                if (weixinIsbind == 1) {
                    weixinRelativeid = uid;
                    bindSocialAccountWeixinTv.setText("已绑定");
                } else {
                    bindSocialAccountWeixinTv.setText("未绑定");
                }
                break;
            case 2:
                qqIsbind = type;
                if (qqIsbind == 1) {
                    qqRelativeid = uid;
                    bindSocialAccountQqTv.setText("已绑定");
                } else {
                    bindSocialAccountQqTv.setText("未绑定");
                }
                break;
            case 3:
                weiboIsbind = type;
                if (weiboIsbind == 1) {
                    weiboRelativeid = uid;
                    bindSocialAccountWeiboTv.setText("已绑定");
                } else {
                    bindSocialAccountWeiboTv.setText("未绑定");
                }
                break;
            case 4:
                xiaomiIsbind = type;
                if (xiaomiIsbind == 1) {
                    xiaomiRelativeid = uid;
                    bindSocialAccountXiaomiTv.setText("已绑定");
                } else {
                    bindSocialAccountXiaomiTv.setText("未绑定");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dissmissLoading() {
        dismissLoadDialog();
    }

    private void doBindAccount(String uid, String nickname, String avatar, String apptype) {
        presenter.doUserBindOuterAccount(uid, nickname, avatar, apptype);
    }

    private void doUnBind(String apptype, String uid) {
        presenter.doUserOuterSysUnBind(apptype, uid);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
